#include "lru.h"

#if 0
namespace herm{

Lru::Lru(size_t maxSize)
{
    m_maxSize = maxSize;
}

bool Lru::Update(uint32_t key, uint32_t& removingKey)
{
    bool isRemoving = false;
    removingKey = 0;
    Key2Node::const_iterator it = m_key2Node.find(key);
    if (it != m_key2Node.end())
    {
        KeyNode* node = it->second;
        // If the update node is back in the update list, do nothing
        if (!m_updateList.IsBack(node))
        {
            m_updateList.Erase(node);
            m_updateList.PushBack(node);
        }
    }
    else
    {
        if (m_key2Node.size() >= m_maxSize)
        {
            KeyNode* node = m_updateList.PopFront();
            if (node)
            {
                isRemoving = true;
                removingKey = node->key;
                m_key2Node.erase(node->key);

                m_nodeAlloc.Free(node);
            }
        }

        KeyNode* node = m_nodeAlloc.Malloc();
        node->key = key;
        m_updateList.PushBack(node);
        m_key2Node.insert(std::make_pair(key, node));
    }

    return isRemoving;
}
}
#endif
